//
//  MSG_BOX_View.m
//  ICE_Player
//
//  Created by xl on 11-7-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "MSG_BOX_View.h"
#import "Globalize.h"

@implementation MSG_BOX_View

@synthesize ok_btn;
@synthesize cancel_btn;
@synthesize title_lb;
@synthesize msg_lb;
@synthesize ok_lb;
@synthesize cancel_lb;
@synthesize back_view;


- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
		ret_callback = nil;
        // Initialization code.
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
    /*CREATE_ICE_VIEW(back_view, ICE_View, controller);
	CREATE_ICE_VIEW(ok_btn, MSG_BOX_OK_BTN, controller);
	CREATE_ICE_VIEW(cancel_btn, MSG_BOX_CANCEL_BTN, controller);
	CREATE_ICE_VIEW(msg_lb, ICE_Label_View, controller);*/
    [super create_SubViews:controller];
    
    CREATE_ICE_VIEW_GLOBALIZE(ok_lb, ICE_Label_View, controller, EG_LBL_ID_MB_OK);
    CREATE_ICE_VIEW_GLOBALIZE(cancel_lb, ICE_Label_View, controller, EG_LBL_ID_MB_CANCEL);
    
    [self addSubview:back_view];
    [self addSubview:msg_lb];
    [self addSubview:title_lb];
    [self addSubview:ok_lb];
    [self addSubview:cancel_lb];
    [self addSubview:cancel_btn];
    [self addSubview:ok_btn];
    self.hidden = TRUE;
    //ok_lb.hidden = TRUE;
    //cancel_lb.hidden = TRUE;
    
    ok_lb.text = IGS(EG_STR_ID_MB_OK);
    cancel_lb.text = IGS(EG_STR_ID_MB_CANCEL);
}

- (void)destroy_SubViews
{
    RELEASE_ICE_VIEW(back_view);
	RELEASE_ICE_VIEW(ok_btn);
	RELEASE_ICE_VIEW(cancel_btn);
	RELEASE_ICE_VIEW(msg_lb);
    RELEASE_ICE_VIEW(title_lb);
    RELEASE_ICE_VIEW(ok_lb);
    RELEASE_ICE_VIEW(cancel_lb);
}

- (void) setHidden:(BOOL)enabled
{
	if (enabled)
	{
		cancel_btn.hidden = enabled;
	}
	ok_btn.hidden = enabled;
    
    if (enabled)
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
        
    }
    else
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
    }
    
	super.hidden = enabled;
}

#define AJUST_CGRECT_POINT(r) {r.origin.x = (CGFloat)((long)r.origin.x),\
                                r.origin.y = (CGFloat)((long)r.origin.y),\
                                r.size.width = (CGFloat)((long)r.size.width),\
                                r.size.height = (CGFloat)((long)r.size.height)\
                                ;};

- (void) Show_Box_NSString:(MSG_BOX_TYPE)box_type  title_content:(NSString*)title msg_content:(NSString*)msg_str parent_view:(UIView*)parent result_obj:(id)obj result_notify:(SEL)method
{
    CGRect rect = {0};
    CGRect s_rect = {0};
	if (!self.hidden)
	{
		return;
	}
	if (parent && parent != self.superview)
	{
		[self removeFromSuperview];
		[parent addSubview:self];
	}
    
    title_lb.text = title;
    msg_lb.text = msg_str;
    ok_lb.text = IGS(EG_STR_ID_MB_OK);
    cancel_lb.text = IGS(EG_STR_ID_MB_CANCEL);
    
    rect = parent.bounds;
    
    rect.origin.x = rect.origin.x + (rect.size.width / 2) - (self.frame.size.width / 2);
    rect.origin.y = rect.origin.y + (rect.size.height * 3 / 7) - (self.frame.size.height / 2);// - 60;
    rect.size.width = self.frame.size.width;
    rect.size.height = self.frame.size.height;
    
    AJUST_CGRECT_POINT(rect);
    s_rect = rect;
    self.frame = s_rect;
    //self.frame = CGRectMake(0.f, 0.f, rect.size.width, rect.size.height);
	if (self.superview)
	{
        ret_obj = obj;
		ret_callback = method;
	}
	switch (box_type)
	{
		case MT_UNKNOWN_BOX:
        case MT_WIFI_BOX:
		case MT_SINGLE_BTN_BOX:
			//set button
            rect = ok_btn.frame;
			rect.origin.x = back_view.frame.origin.x + (back_view.frame.size.width / 2) - (ok_btn.frame.size.width / 2);
			rect.origin.y = back_view.frame.origin.y + (back_view.frame.size.height * 3 / 5);
            AJUST_CGRECT_POINT(rect);
			ok_btn.frame = rect;
            ok_btn.hidden = NO;
            //set label
            rect = ok_lb.frame;
            rect.origin.x = ok_btn.frame.origin.x + ((ok_btn.frame.size.width -  ok_lb.frame.size.width) / 2);
            rect.origin.y = ok_btn.frame.origin.y;
            AJUST_CGRECT_POINT(rect);
            ok_lb.frame = rect;
            ok_lb.hidden = NO;
            cancel_lb.hidden = YES;
			break;
        
        case MT_YESNO_BTN_BOX:
            ok_lb.text = IGS(EG_STR_ID_MB_YES);
            cancel_lb.text = IGS(EG_STR_ID_MB_NO);
		case MT_FILE_DELETE_BOX:
		case MT_DOUBLE_BTN_BOX:
            //set buttons
			rect = ok_btn.frame;
			rect.origin.x = back_view.frame.origin.x + (back_view.frame.size.width * 9 / 20) - ok_btn.frame.size.width;
			rect.origin.y = back_view.frame.origin.y + (back_view.frame.size.height * 3 / 5);
            AJUST_CGRECT_POINT(rect);
			ok_btn.frame = rect;
			
			rect = cancel_btn.frame;
			rect.origin.x = back_view.frame.origin.x + (back_view.frame.size.width * 11 / 20);
			rect.origin.y = back_view.frame.origin.y + (back_view.frame.size.height * 3 / 5);
            AJUST_CGRECT_POINT(rect);
			cancel_btn.frame = rect;
			
			cancel_btn.hidden = NO;
            //set labels
            rect = ok_lb.frame;
            rect.origin.x = ok_btn.frame.origin.x + ((ok_btn.frame.size.width -  ok_lb.frame.size.width) / 2);
            rect.origin.y = ok_btn.frame.origin.y;
            AJUST_CGRECT_POINT(rect);
            ok_lb.frame = rect;
            ok_lb.hidden = NO;
            
            rect = cancel_lb.frame;
            rect.origin.x = cancel_btn.frame.origin.x + ((cancel_btn.frame.size.width -  cancel_lb.frame.size.width) / 2);
            rect.origin.y = cancel_btn.frame.origin.y;
            AJUST_CGRECT_POINT(rect);
            cancel_lb.frame = rect;
            cancel_lb.hidden = NO;
            
			break;
	}
    
    //set title position
    rect = title_lb.frame;
    rect.origin.x = back_view.frame.origin.x + (back_view.frame.size.width * 19 / 40) - (title_lb.frame.size.width / 2);
    rect.origin.y = back_view.frame.origin.y + (back_view.frame.size.height * 1 / 8);
    AJUST_CGRECT_POINT(rect);
    title_lb.frame = rect;
    //set msg string position
    rect = msg_lb.frame;
    rect.origin.x = back_view.frame.origin.x + (back_view.frame.size.width * 19 / 40) - (msg_lb.frame.size.width / 2);
    rect.origin.y = title_lb.frame.origin.y + title_lb.frame.size.height + (back_view.frame.size.height * 1 / 20);
    AJUST_CGRECT_POINT(rect);
    msg_lb.frame = rect;
    
    back_view.hidden = NO;
    
	self.hidden = NO;
}


- (void) Show_Box:(MSG_BOX_TYPE)box_type  title_content:(ICE_String*)title msg_content:(ICE_String*)msg_str parent_view:(UIView*)parent result_obj:(id)obj result_notify:(SEL)method
{
    NSString* t_str = [[NSString alloc] initWithUTF8String:title.string];
    NSString* m_str = [[NSString alloc] initWithUTF8String:msg_str.string];
    
    [self Show_Box_NSString:box_type title_content:t_str msg_content:m_str parent_view:parent result_obj:obj result_notify:method];
    
    [t_str release];
    [m_str release];
}

- (void) Hide_Box
{
	self.hidden = YES;
}

- (IBAction)On_OK_BTN_Click:(id)sender
{
	if (ret_callback && self.superview)
	{
		[ret_obj performSelector:ret_callback withObject:YES];
	}
	[self Hide_Box];
}

- (IBAction)On_CANCEL_BTN_Click:(id)sender
{
	if (ret_callback && self.superview)
	{
		[ret_obj performSelector:ret_callback withObject:NO];
	}
	[self Hide_Box];
}

@end
